var app = getApp();
Page({
  data: {
    //商品列表
    shoppingCarList: [],
    //编辑删除
    handle: '编辑',
    //结算删除
    handle2: '结算',
    //总价
    totalQuantity: 0,
    //全选
    allSel: false
  },
  onLoad: function(options) {
    var shoppingCarList = app.globalData.shoppingCarList;
    for (var i of shoppingCarList) {
      i.sel = false;
    }
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
      shoppingCarList: shoppingCarList
    })
  },
  //返回下一个页面
  rollback: function() {
    var pages = getCurrentPages();
    var page = pages[pages.length - 2];
    page.setData({
      shoppingCarQuantity: app.globalData.shoppingCarList.length
    })
    wx.navigateBack({
      delta: 1
    })
  },
  //全选全不选
  all: function() {
    var allSel = this.data.allSel;
    var shoppingCarList = this.data.shoppingCarList;
    var totalQuantity = 0;
    if (allSel) {
      for (var i of shoppingCarList) {
        i.sel = false;
      }
      allSel = false;
    } else {
      for (var i of shoppingCarList) {
        i.sel = true;
        totalQuantity += i.quantity * i.price;
      }
      allSel = true;
    }
    this.setData({
      shoppingCarList: shoppingCarList,
      allSel: allSel,
      totalQuantity: parseFloat(totalQuantity).toFixed(2)
    })
  },
  //单选
  sel: function(e) {
    var allSel = this.data.allSel;
    var shoppingCarList = this.data.shoppingCarList;
    var totalQuantity = parseFloat(this.data.totalQuantity);
    var index = e.currentTarget.dataset.index;
    var goods = shoppingCarList[index];
    if (goods.sel) {
      totalQuantity -= goods.price * goods.quantity;
      goods.sel = false;
      allSel = false;
      this.setData({
        allSel: false
      })
    } else {
      totalQuantity += goods.price * goods.quantity;
      goods.sel = true;
      this.isAllSel();
    }
    this.setData({
      shoppingCarList: shoppingCarList,
      totalQuantity: parseFloat(totalQuantity).toFixed(2)
    })
  },
  //是否是全选
  isAllSel: function() {
    var count = 0;
    var shoppingCarList = this.data.shoppingCarList;
    var allSel = false;
    for (var i of shoppingCarList) {
      if (i.sel) {
        count += 1;
      }
    }

    if (count != 0 && count == shoppingCarList.length) {
      allSel = true;
    }
    this.setData({
      allSel: allSel
    })
  },
  //编辑删除
  handle: function() {
    var handle = this.data.handle;
    if (handle == '编辑') {
      this.setData({
        handle: '完成',
        handle2: '删除'
      })
    } else {
      this.setData({
        handle: '编辑',
        handle2: '结算'
      })
    }
  },
  //下单或者删除
  delOrorder: function() {
    var handle2 = this.data.handle2;
    var shoppingCarList = this.data.shoppingCarList;
    var that = this;
    if (handle2 == '结算') {
      console.log('结算')
    } else {
      var specIds = [];
      for (var i of shoppingCarList) {
        if (i.sel) {
          specIds.push(i.specId);
        }
      }
      if (specIds.length == 0) {
        return;
      }
      wx.request({
        url: app.globalData.requestUrl + 'deleteGoods',
        data: {
          specIds: specIds,
          userId: app.globalData.user.id
        },
        method: 'POST',
        success: function(r) {
          if (r.data.code == 200) {
            var shoppingCarList = r.data.data;
            app.globalData.shoppingCarList = shoppingCarList;
            for (var i of shoppingCarList) {
              i.sel = false;
            }
            that.setData({
              shoppingCarList: shoppingCarList,
              totalQuantity:0
            })
            that.isAllSel();

          } else {
            wx.showToast({
              title: '移除商品失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function() {
          wx.showToast({
            title: '网络断开连接,移除商品失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  //增加减少
  mathQuantity: function(e) {
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    var shoppingCarList = this.data.shoppingCarList;
    var goods = shoppingCarList[index];
    if (type == 'jia') {
      goods.quantity = parseInt(goods.quantity) + 1;
    } else {
      if (goods.quantity == 1) {
        return;
      }
      goods.quantity = parseInt(goods.quantity) - 1;
    }
    this.updateGoodsQuantity(goods, index, type);
  },
  //更改数量
  updateGoodsQuantity: function(goods, index, type) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'updateGoodsQuantity',
      data: {
        userId: app.globalData.user.id,
        goodsId: goods.goodsId,
        quantity: goods.quantity,
        spec: goods.specId
      },
      method: 'POST',
      success: function(r) {
        if (r.data.code == 200) {
          var shoppingCarList = that.data.shoppingCarList;
          shoppingCarList[index].quantity = goods.quantity;
          var totalQuantity = parseFloat(that.data.totalQuantity);
          if (goods.sel) {
            if (type == 'jia') {
              totalQuantity += parseFloat(goods.price);
            } else {
              totalQuantity -= parseFloat(goods.price);
            }
          }
          that.setData({
            shoppingCarList: shoppingCarList,
            totalQuantity: parseFloat(totalQuantity).toFixed(2)
          })
          console.log(that.data.shoppingCarList)
          app.globalData.shoppingCarList = r.data.data
        } else {
          wx.showToast({
            title: '更改商品数量失败',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,更改商品数量失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})